package com.ruoyi.edit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edit.domain.*;
import com.ruoyi.edit.service.*;
import com.ruoyi.edit.utils.AssertUtil;
import com.ruoyi.edit.utils.CopyUtils;
import com.ruoyi.edit.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edit.mapper.CourseContentMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动课内容Service业务层处理
 *
 * @author qiufeng
 * @date 2022-03-19
 */
@Service
public class CourseContentServiceImpl implements ICourseContentService {
    @Autowired
    private CourseContentMapper courseContentMapper;

    @Autowired
    private ICoursePackageService coursePackageService;

    @Autowired
    private ICourseContentService courseContentService;

    @Autowired
    private IVendorCourseService vendorCourseService;

    @Autowired
    private ICoursePackageV2Service coursePackageV2Service;

    @Autowired
    private ITcVendorCourseUserRecordService tcVendorCourseUserRecordService;

    /**
     * 查询活动课内容
     *
     * @param id 活动课内容主键
     * @return 活动课内容
     */
    @Override
    public CourseContent selectCourseContentById(String id) {
        return courseContentMapper.selectCourseContentById(id);
    }

    /**
     * 查询活动课内容列表
     *
     * @param courseContent 活动课内容
     * @return 活动课内容
     */
    @Override
    public List<CourseContent> selectCourseContentList(CourseContent courseContent) {
        return courseContentMapper.selectCourseContentList(courseContent);
    }

    /**
     * 新增活动课内容
     *
     * @param courseContent 活动课内容
     * @return 结果
     */
    @Override
    public CourseContent insertCourseContent(CourseContent courseContent) {
        if (StringUtils.equals(courseContent.getType(), "") || StringUtils.equals(courseContent.getType(), "0")) {
            courseContent.setType("txt");
        }
        courseContent.setId(UuidUtil.getUuid());
        courseContent.setDeleted(0);
        courseContent.setCreated(new Date());
        courseContent.setCreatedBy(ShiroUtils.getLoginName());
        courseContent.setCreatedName(ShiroUtils.getName());
        courseContentMapper.insertCourseContent(courseContent);
        return courseContent;

    }

    /**
     * 修改活动课内容
     *
     * @param courseContent 活动课内容
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int updateCourseContent(CourseContent courseContent) {
        if (StringUtils.equals(courseContent.getType(), "") || StringUtils.equals(courseContent.getType(), "0")) {
            courseContent.setType("txt");
        }

        TcVendorCourseUserRecord tcVendorCourseUserRecord = new TcVendorCourseUserRecord();
        tcVendorCourseUserRecord.setVendorCourseId(courseContent.getCourseId());
        tcVendorCourseUserRecordService.insertTcVendorCourseUserRecord(tcVendorCourseUserRecord);
        return courseContentMapper.updateCourseContent(courseContent);
    }

    /**
     * 批量删除活动课内容
     *
     * @param ids 需要删除的活动课内容主键
     * @return 结果
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public int deleteCourseContentByIds(String ids) {
        String[] ids1 = Convert.toStrArray(ids);
        for (int i = 0; i < ids1.length; i++) {
            CourseContent courseContent = courseContentMapper.selectCourseContentById(ids1[i]);
            if (AssertUtil.isNotEmpty(courseContent)) {
                courseContent.setDeleted(1);
                courseContentMapper.updateCourseContent(courseContent);
            }
        }
        return 1;
    }

    /**
     * 删除活动课内容信息
     *
     * @param id 活动课内容主键
     * @return 结果
     */
    @Override
    public int deleteCourseContentById(String id) {
        return courseContentMapper.deleteCourseContentById(id);
    }

    @Override
    public CourseContent getVendorPackageIdAndType(String vendorPackageId, String type) {
        return courseContentMapper.getVendorPackageIdAndType(vendorPackageId, type);
    }


    @Override
    public int addAll(CourseContent courseContent) {
        CourseContent courseContent1 = courseContentMapper.getVendorPackageIdAndType(courseContent.getVendorPackageId(), courseContent.getType());
        if (AssertUtil.isEmpty(courseContent1)) {
            courseContent.setId(UuidUtil.getUuid());
            courseContent.setDeleted(0);
            courseContent.setCreated(new Date());
            return courseContentMapper.insertCourseContent(courseContent);
        } else {
            courseContent1.setContent(courseContent.getContent());
            return courseContentMapper.updateCourseContent(courseContent1);
        }
    }

    @Override
    public List<CourseContent> getVendorPackageId(String vendorPackageId) {
        return courseContentMapper.getVendorPackageId(vendorPackageId);
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public void copyWeekPackages(String id, String packagesId) {
        CoursePackage copyPackage = coursePackageService.selectCoursePackageById(packagesId);
        CoursePackage coursePackage = new CoursePackage();
        coursePackage.setName(copyPackage.getName() + ":复本");
        coursePackage.setParentId(id);
        coursePackage = coursePackageService.insertCoursePackage(coursePackage);
        List<CourseContent> list = courseContentService.getVendorPackageId(packagesId);
        for (int i = 0; i < list.size(); i++) {
            CourseContent courseContent = list.get(i);
            String id1 = courseContent.getId();
            if (StringUtils.equals(courseContent.getParentId(), "0")) {
                courseContent.setVendorPackageId(coursePackage.getId());
                courseContentService.insertCourseContent(courseContent);
                if (courseContent.getParentType() == 1) {
                    CourseContent courseContent2 = new CourseContent();
                    courseContent2.setParentId(id1);
                    courseContent2.setVendorPackageId(copyPackage.getId());
                    List<CourseContent> courseContentList1 = courseContentService.selectCourseContentList(courseContent2);
                    for (int k = 0; k < courseContentList1.size(); k++) {
                        CourseContent courseContent3 = courseContentList1.get(k);
                        courseContent3.setVendorPackageId(coursePackage.getId());
                        courseContent3.setParentId(courseContent.getId());
                        courseContentService.insertCourseContent(courseContent3);
                    }
                }
            }
        }
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public void copyWeekCoursePackages(String id, String packagesId) {
        CoursePackage copyPackage = coursePackageService.selectCoursePackageById(packagesId);
        VendorCourse vendorCourse = new VendorCourse();
        vendorCourse.setCoursePackageId(copyPackage.getId());
        List<VendorCourse> list = vendorCourseService.selectVendorCourseList(vendorCourse);
        for (int i = 0; i < list.size(); i++) {
            VendorCourse vendorCourse1 = list.get(i);
            String courseId = vendorCourse1.getId();
            vendorCourse1.setCoursePackageId(id);
            VendorCourse course = vendorCourseService.insertVendorCourse(vendorCourse1);

            if (AssertUtil.isNotEmpty(list.get(i).getId())) {
                CourseContent courseContent = new CourseContent();
                courseContent.setParentId(String.valueOf(0));
                courseContent.setCourseId(courseId);
                List<CourseContent> courseContentList = courseContentService.selectCourseContentList(courseContent);
                for (int j = 0; j < courseContentList.size(); j++) {
                    CourseContent courseContent1 = courseContentList.get(j);
                    String id1 = courseContent1.getId();
                    if (StringUtils.equals(courseContent.getParentId(), "0")) {
                        courseContent1.setCourseId(course.getId());
                        courseContentService.insertCourseContent(courseContent1);
                        if (courseContent1.getParentType() == 1) {
                            CourseContent courseContent2 = new CourseContent();
                            courseContent2.setCourseId(courseId);
                            courseContent2.setParentId(id1);
                            List<CourseContent> courseContentList1 = courseContentService.selectCourseContentList(courseContent2);
                            for (int k = 0; k < courseContentList1.size(); k++) {
                                CourseContent courseContent3 = courseContentList1.get(k);
                                courseContent3.setParentId(courseContent1.getId());
                                courseContent3.setCourseId(course.getId());
                                courseContentService.insertCourseContent(courseContent3);
                            }
                        }
                    }

                }
            }
        }
    }


    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public void copyWeekPackagesV2(String courseId, String packagesId) {
        CoursePackage copyPackage = coursePackageService.selectCoursePackageById(packagesId);
        List<CourseContent> list = courseContentService.getVendorPackageId(packagesId);
        for (int i = 0; i < list.size(); i++) {
            CourseContent courseContent = list.get(i);
            courseContent.setVendorPackageId(null);
            String id1 = courseContent.getId();
            if (StringUtils.equals(courseContent.getParentId(), "0")) {
                courseContent.setCourseId(courseId);
                courseContent.setDetailsType(1);
                courseContentService.insertCourseContent(courseContent);
                if (courseContent.getParentType() == 1) {
                    CourseContent courseContent2 = new CourseContent();
                    courseContent2.setParentId(id1);
                    courseContent2.setVendorPackageId(copyPackage.getId());
                    List<CourseContent> courseContentList1 = courseContentService.selectCourseContentList(courseContent2);
                    for (int k = 0; k < courseContentList1.size(); k++) {
                        CourseContent courseContent3 = courseContentList1.get(k);
                        courseContent3.setVendorPackageId(null);
                        courseContent3.setCourseId(courseId);
                        courseContent3.setParentId(courseContent.getId());
                        courseContent3.setDetailsType(1);
                        courseContentService.insertCourseContent(courseContent3);
                    }
                }
            }
        }
    }


    @Override
    public void copyPackagesV2(String courseId, String packagesId) {
        CoursePackageV2 copyPackage = coursePackageV2Service.selectCoursePackageV2ById(packagesId);
        List<CourseContent> list = courseContentService.getVendorPackageId(packagesId);
        List<CourseContent> list_1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CourseContent courseContent = list.get(i);
            String id = list.get(i).getId();
            courseContent.setVendorPackageId(null);
            if (StringUtils.equals(courseContent.getParentId(), "0")) {
                courseContent.setCourseId(courseId);
                courseContent.setDetailsType(1);
                courseContent.setCategoryType(Integer.valueOf(copyPackage.getTypeName()));
                courseContent.setId(UuidUtil.getUuid());
                courseContent.setDeleted(0);
                courseContent.setCreated(new Date());
                courseContent.setCreatedBy(ShiroUtils.getLoginName());
                courseContent.setCreatedName(ShiroUtils.getName());
                list_1.add(courseContent);
//                courseContent= courseContentService.insertCourseContent(courseContent);
                if (courseContent.getParentType() == 1) {
                    allInsert(id, courseContent.getId(), courseId, copyPackage.getId(), list);
                }

            }
        }
        if (list_1.size() > 0) {
            courseContentMapper.batchCourseContent(list_1);
        }

    }

    public void allInsert(String id, String newId, String courseId, String packageId, List<CourseContent> list) {
        CourseContent courseContent2 = new CourseContent();
        courseContent2.setParentId(id);
        courseContent2.setVendorPackageId(packageId);
        List<CourseContent> list_1 = new ArrayList<>();
        List<CourseContent> courseContentList1 = getCourseContent(id, list);
        for (int k = 0; k < courseContentList1.size(); k++) {
            String id1 = courseContentList1.get(k).getId();
            CourseContent courseContent3 = courseContentList1.get(k);
            courseContent3.setVendorPackageId(null);
            courseContent3.setCourseId(courseId);
            courseContent3.setParentId(newId);
            courseContent3.setDetailsType(1);
            courseContent3.setId(UuidUtil.getUuid());
            courseContent3.setDeleted(0);
            courseContent3.setCreated(new Date());
            courseContent3.setCreatedBy(ShiroUtils.getLoginName());
            courseContent3.setCreatedName(ShiroUtils.getName());
            list_1.add(courseContent3);
//            courseContentService.insertCourseContent(courseContent3);
            allInsert(id1, courseContent3.getId(), courseId, packageId, list);
        }
        if (list_1.size() > 0) {
            courseContentMapper.batchCourseContent(list_1);
        }

    }

    public List<CourseContent> getCourseContent(String id, List<CourseContent> courseContentList) {
        List<CourseContent> list = new ArrayList<>();
        for (int i = 0; i < courseContentList.size(); i++) {
            if (StringUtils.equals(id, courseContentList.get(i).getParentId())) {
                list.add(courseContentList.get(i));
            }
        }
        return list;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public void copyStructure(String coursePackageV2Id) {
        CoursePackageV2 copyPackage = coursePackageV2Service.selectCoursePackageV2ById(coursePackageV2Id);
        copyPackage.setId(UuidUtil.getUuid());
        copyPackage.setName(copyPackage.getName()+"-拷贝");
        coursePackageV2Service.insertCoursePackageV2(copyPackage);
        List<CourseContent> list = courseContentService.getVendorPackageId(coursePackageV2Id);
        List<CourseContent> list_1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CourseContent courseContent = list.get(i);
            String id = list.get(i).getId();
            courseContent.setVendorPackageId(copyPackage.getId());
            if (StringUtils.equals(courseContent.getParentId(), "0")) {
                courseContent.setDetailsType(1);
                courseContent.setCategoryType(Integer.valueOf(copyPackage.getTypeName()));
                courseContent.setId(UuidUtil.getUuid());
                courseContent.setDeleted(0);
                courseContent.setCreated(new Date());
                courseContent.setCreatedBy(ShiroUtils.getLoginName());
                courseContent.setCreatedName(ShiroUtils.getName());
                list_1.add(courseContent);
                if (courseContent.getParentType() == 1) {
                    allCopyStructure(id, courseContent.getId(), copyPackage.getId(), list);
                }
            }
        }
        if (list_1.size() > 0) {
            courseContentMapper.batchCourseContent(list_1);
        }
    }

    public void allCopyStructure(String id, String newId, String packageId, List<CourseContent> list) {
        List<CourseContent> list_1 = new ArrayList<>();
        List<CourseContent> courseContentList1 = getCourseContent(id, list);
        for (int k = 0; k < courseContentList1.size(); k++) {
            String id1 = courseContentList1.get(k).getId();
            CourseContent courseContent3 = courseContentList1.get(k);
            courseContent3.setId(UuidUtil.getUuid());
            courseContent3.setVendorPackageId(packageId);
            courseContent3.setParentId(newId);
            courseContent3.setDetailsType(1);
            courseContent3.setDeleted(0);
            courseContent3.setCreated(new Date());
            courseContent3.setCreatedBy(ShiroUtils.getLoginName());
            courseContent3.setCreatedName(ShiroUtils.getName());
            list_1.add(courseContent3);
            allCopyStructure(id1, courseContent3.getId(), packageId, list);
        }
        if (list_1.size() > 0) {
            courseContentMapper.batchCourseContent(list_1);
        }
    }
}